package com.lalifa.googledate.ui.adapter

import android.annotation.SuppressLint
import android.content.res.Resources
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.ViewPager2
import com.drake.brv.BindingAdapter
import com.drake.brv.annotaion.DividerOrientation
import com.drake.brv.utils.divider
import com.drake.brv.utils.grid
import com.drake.brv.utils.linear
import com.drake.brv.utils.setup
import com.lalifa.extension.PATTERN_HOUR_MINUTE
import com.lalifa.extension.dp
import com.lalifa.extension.format
import com.lalifa.extension.invisible
import com.lalifa.extension.visible
import com.lalifa.googledate.R
import com.lalifa.googledate.api.CalendarBean
import com.lalifa.googledate.api.ChooseTimeBean
import com.lalifa.googledate.api.ColorBean
import com.lalifa.googledate.api.CountryBean
import com.lalifa.googledate.api.EventListBean
import com.lalifa.googledate.api.EventModel
import com.lalifa.googledate.databinding.EmojiItemBinding
import com.lalifa.googledate.databinding.ItemCalendarBinding
import com.lalifa.googledate.databinding.ItemChooseTime0Binding
import com.lalifa.googledate.databinding.ItemChooseTimeBinding
import com.lalifa.googledate.databinding.ItemColorBinding
import com.lalifa.googledate.databinding.ItemCountryBinding
import com.lalifa.googledate.databinding.ItemEventBinding
import com.lalifa.googledate.databinding.ItemRow1Binding
import com.lalifa.googledate.databinding.ItemRowBinding
import com.lalifa.googledate.databinding.ItemTextBinding
import com.lalifa.googledate.databinding.ViewItemBinding
import com.lalifa.googledate.util.Tool
import com.lalifa.utils.UIKit

/**
 * 语音房消息列表
 */
@SuppressLint("SetTextI18n")
fun RecyclerView.eventAdapter(): BindingAdapter {
    return linear().setup {
        addType<EventModel> { R.layout.view_item }
        onBind {
            val bean = getModel<EventModel>()
            if (itemViewType == R.layout.view_item) {
                getBinding<ViewItemBinding>().apply {
                    itemEventName.text = ""
                }
            }
        }
    }
}

fun ViewPager2.emojiAdapter2(
    isColor: Boolean,
    clickCallBack: (Int, String) -> Unit,
    callback: BindingAdapter.() -> Unit
): ViewPager2 {
    val bindingAdapter = BindingAdapter().apply {
        addType<ArrayList<String>>(R.layout.emoji_item)
        onBind {
            getBinding<EmojiItemBinding>().apply {
                if (isColor) {
                    recyclerView.emojiListAdapter1().apply {
                        R.id.iv_im.onClick {
                            val model1 = getModel<String>()
                            clickCallBack.invoke(1, model1)
                        }
                    }.models = getModel<ArrayList<String>>()
                } else {
                    recyclerView.emojiListAdapter().apply {
                        R.id.iv_icon.onClick {
                            val model1 = getModel<String>()
                            clickCallBack.invoke(1, model1)
                        }
                    }.models = getModel<ArrayList<String>>()
                }
            }
        }
    }
    adapter = bindingAdapter
    callback.invoke(bindingAdapter)
    return this
}

@SuppressLint("DiscouragedApi", "UseCompatLoadingForDrawables")
fun RecyclerView.emojiListAdapter(): BindingAdapter {
    return grid(5).divider {
        setDivider(3.dp)
        orientation = DividerOrientation.VERTICAL
    }.divider {
        setDivider(3.dp)
        orientation = DividerOrientation.HORIZONTAL
    }.setup {
        addType<String>(R.layout.item_row)
        onBind {
            val bean = getModel<String>()
            getBinding<ItemRowBinding>().apply {
                val resources: Resources = UIKit.getResources()
                try {
                    // 根据图片名称获取对应的 ID
                    val resourceId: Int =
                        resources.getIdentifier(bean, "drawable", context.packageName)
                    if (resourceId != 0) {
                        val drawable: Drawable = resources.getDrawable(resourceId)
                        ivIcon.background = drawable
                    }
                } catch (e: Exception) {
                //    LogUtils.i("加载本地资源发生错误：" + e.message)
                }
            }
        }
    }
}


@SuppressLint("DiscouragedApi", "UseCompatLoadingForDrawables")
fun RecyclerView.emojiListAdapter1(): BindingAdapter {
    return grid(5).divider {
        setDivider(3.dp)
        orientation = DividerOrientation.VERTICAL
    }.divider {
        setDivider(3.dp)
        orientation = DividerOrientation.HORIZONTAL
    }.setup {
        addType<String>(R.layout.item_row1)
        onBind {
            val bean = getModel<String>()
            getBinding<ItemRow1Binding>().apply {
                val shape = GradientDrawable()
                shape.setColor(Color.parseColor(bean))
                ivIm.background = shape
            }
        }
    }
}

fun RecyclerView.addNoticeAdapter(): BindingAdapter {
    return linear().setup {
        addType<ChooseTimeBean>(R.layout.item_choose_time0)
        onBind {
            val bean = getModel<ChooseTimeBean>()
            getBinding<ItemChooseTime0Binding>().apply {
                if (modelPosition == 0 && bean.time != 0) {
                    ld.visible()
                } else {
                    ld.invisible()
                }
                remove.isVisible = modelPosition != models!!.size - 1
                tvTime.text = when (bean.type) {
                    1 -> "${context.getString(R.string.notify_email)}${bean.timeStr}${
                        context.getString(
                            R.string.notify1
                        )
                    }"

                    2 -> "${context.getString(R.string.notify_sms)}${bean.timeStr}${
                        context.getString(
                            R.string.notify1
                        )
                    }"

                    else -> bean.timeStr
                }

            }
        }
    }
}

fun RecyclerView.addNoticeTimeAdapter(): BindingAdapter {
    return linear().setup {
        addType<ChooseTimeBean>(R.layout.item_choose_time)
        onBind {
            val bean = getModel<ChooseTimeBean>()
            getBinding<ItemChooseTimeBinding>().apply {
                tvTime.text = bean.timeStr
            }
        }
    }
}

fun RecyclerView.tvAdapter(): BindingAdapter {
    return linear().setup {
        addType<String>(R.layout.item_text)
        onBind {
            val bean = getModel<String>()
            getBinding<ItemTextBinding>().apply {
                tv.text = bean
            }
        }
    }
}

fun RecyclerView.colorAdapter(): BindingAdapter {
    return linear().setup {
        addType<ColorBean>(R.layout.item_color)
        onBind {
            val bean = getModel<ColorBean>()
            getBinding<ItemColorBinding>().apply {
                typeIm.setBackgroundColor(Color.parseColor(bean.value))
                typeName.text = bean.name
            }
        }
    }
}

fun RecyclerView.countryAdapter(): BindingAdapter {
    return linear().setup {
        addType<CountryBean>(R.layout.item_country)
        onBind {
            val bean = getModel<CountryBean>()
            getBinding<ItemCountryBinding>().apply {
                countryName.text = bean.name
                type.text = when(bean.isPublic){
                    0->context.getString(R.string.not_show)
                    else->context.getString(R.string.public_holiday)
                }
            }
        }
    }
}

fun RecyclerView.calendarAdapter(): BindingAdapter {
    return linear(orientation = RecyclerView.HORIZONTAL).setup {
        addType<CalendarBean>(R.layout.item_calendar)
        onBind {
            val bean = getModel<CalendarBean>()
            getBinding<ItemCalendarBinding>().apply {
                if (bean.icon.isNotEmpty()) {
                    val resources: Resources = UIKit.getResources()
                    try {
                        // 根据图片名称获取对应的 ID
                        val resourceId: Int =
                            resources.getIdentifier(bean.icon, "drawable", context.packageName)
                        if (resourceId != 0) {
                            val drawable: Drawable = resources.getDrawable(resourceId)
                            imCalendar.background = drawable
                        }
                    } catch (e: Exception) {
                      //  LogUtils.i("加载本地资源发生错误：" + e.message)
                    }
                }
                tvCalendar.text = bean.name
            }
        }
    }
}

fun RecyclerView.calendarInfoAdapter(): BindingAdapter {
    return linear().setup {
        addType<CalendarBean>(R.layout.item_calendar_info)
        onBind {
            val bean = getModel<CalendarBean>()
            getBinding<ItemCalendarBinding>().apply {
                if (bean.icon.isNotEmpty()) {
                    val resources: Resources = UIKit.getResources()
                    try {
                        // 根据图片名称获取对应的 ID
                        val resourceId: Int =
                            resources.getIdentifier(bean.icon, "drawable", context.packageName)
                        if (resourceId != 0) {
                            val drawable: Drawable = resources.getDrawable(resourceId)
                            imCalendar.background = drawable
                        }
                    } catch (e: Exception) {
                     //   LogUtils.i("加载本地资源发生错误：" + e.message)
                    }
                }
                tvCalendar.text = bean.name
            }
        }
    }
}

fun RecyclerView.searchAdapter(): BindingAdapter {
    return linear().setup {
        addType<EventListBean>(R.layout.item_event)
        onBind {
            val weekStr = arrayOf("MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN")
            val bean = getModel<EventListBean>()
            getBinding<ItemEventBinding>().apply {
                var ss = bean.event.title
                if (bean.event.noofdayevent > 1) {
                    ss = ss + " (" +bean.data.toString("d MMMM") + "-" +
                            bean.data.plusDays(bean.event.noofdayevent - 1)
                                .toString("d MMMM") + ")"
                } else if (!bean.event.isallday) {
                    val start: String = bean.event.starttime.format(PATTERN_HOUR_MINUTE)
                    val end: String = bean.event.endtime.format(PATTERN_HOUR_MINUTE)
                    ss = "$ss ($start-$end)"
                }
                ss = ss + " " + bean.event.address
                tvToday.text = weekStr[bean.data.dayOfWeek - 1]
                tvTodayNum.text = bean.data.dayOfMonth.toString()
                itemEventName.text =ss
                itemEventName.setBackgroundColor(
                    if (bean.event.eventcolor == -1) Color.parseColor(Tool.color) else bean.event.eventcolor
                )
            }
        }
    }
}